package org.study.redis.api.util.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.ScanParams;
import redis.clients.jedis.resps.ScanResult;

import java.util.*;

/**
 * Redis 操作工具类：Hash
 * 参考 {@link redis.clients.jedis.commands.HashCommands}
 *
 * @author doveylovey
 * @email 1135782208@qq.com
 * @date 2022年04月23日
 */
public class RedisHashUtils {
    private RedisHashUtils() {
        throw new AssertionError("不能实例化 RedisHashUtils 工具类");
    }

    public static long hset(String key, String field, String value) {
        long hset;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hset = jedis.hset(key, field, value);
        }
        return hset;
    }

    public static long hset(String key, Map<String, String> hash) {
        long hset;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hset = jedis.hset(key, hash);
        }
        return hset;
    }

    public static long hsetnx(String key, String field, String value) {
        long hsetnx;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hsetnx = jedis.hsetnx(key, field, value);
        }
        return hsetnx;
    }

    public static String hget(String key, String field) {
        String hget = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hget = jedis.hget(key, field);
        }
        return hget;
    }

    public static String hmset(String key, Map<String, String> hash) {
        String hmset;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hmset = jedis.hmset(key, hash);
        }
        return hmset;
    }

    public static List<String> hmget(String key, String... fields) {
        List<String> hmget = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hmget = jedis.hmget(key, fields);
        }
        return hmget;
    }

    public static List<String> hmget(String key, List<String> fields) {
        List<String> hmget = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hmget = jedis.hmget(key, fields.toArray(new String[fields.size()]));
        }
        return hmget;
    }

    public static long hincrBy(String key, String field, long value) {
        long hincrBy;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hincrBy = jedis.hincrBy(key, field, value);
        }
        return hincrBy;
    }

    public static double hincrByFloat(String key, String field, double value) {
        double hincrByFloat;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hincrByFloat = jedis.hincrByFloat(key, field, value);
        }
        return hincrByFloat;
    }

    public static boolean hexists(String key, String field) {
        boolean hexists;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hexists = jedis.hexists(key, field);
        }
        return hexists;
    }

    public static long hdel(String key, String... fields) {
        long hdel;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hdel = jedis.hdel(key, fields);
        }
        return hdel;
    }

    public static long hdel(String key, List<String> fields) {
        long hdel;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hdel = jedis.hdel(key, fields.toArray(new String[fields.size()]));
        }
        return hdel;
    }

    public static long hlen(String key) {
        long hlen;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hlen = jedis.hlen(key);
        }
        return hlen;
    }

    public static Set<String> hkeys(String key) {
        Set<String> hkeys = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hkeys = jedis.hkeys(key);
        }
        return hkeys;
    }

    public static List<String> hvals(String key) {
        List<String> hvals = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hvals = jedis.hvals(key);
        }
        return hvals;
    }

    public static Map<String, String> hgetAll(String key) {
        Map<String, String> hgetAll = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hgetAll = jedis.hgetAll(key);
        }
        return hgetAll;
    }

    public static String hrandfield(String key) {
        String hrandfield = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hrandfield = jedis.hrandfield(key);
        }
        return hrandfield;
    }

    public static List<String> hrandfield(String key, long count) {
        List<String> hrandfield = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hrandfield = jedis.hrandfield(key, count);
        }
        return hrandfield;
    }

    public static List<Map.Entry<String, String>> hrandfieldWithValues(String key, long count) {
        List<Map.Entry<String, String>> hrandfieldWithValues = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hrandfieldWithValues = jedis.hrandfieldWithValues(key, count);
        }
        return hrandfieldWithValues;
    }

    public static ScanResult<Map.Entry<String, String>> hscan(String key, String cursor) {
        ScanResult<Map.Entry<String, String>> hscan = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hscan = jedis.hscan(key, cursor, new ScanParams());
        }
        return hscan;
    }

    public static ScanResult<Map.Entry<String, String>> hscan(String key, String cursor, ScanParams params) {
        ScanResult<Map.Entry<String, String>> hscan = null;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hscan = jedis.hscan(key, cursor, params);
        }
        return hscan;
    }

    public static long hstrlen(String key, String field) {
        long hstrlen;
        try (Jedis jedis = SysJedisPool.getResource()) {
            hstrlen = jedis.hstrlen(key, field);
        }
        return hstrlen;
    }

    /*public static void main(String[] args) {
        System.out.println("hmset：" + hmset("key:hash", new HashMap<>(2) {{
            put("name", "测试");
            put("date", "2022-04-25");
        }}));
        System.out.println("hmget：" + hmget("key:hash", "name", "date"));
        System.out.println("hkeys：" + hkeys("key:hash"));
        System.out.println("hvals：" + hvals("key:hash"));
    }*/
}
